/***********************************************************************
 * Dao Interface :  SysUserDao.java
 * Author:  Bill Vison
 * Purpose: Defines the Hibernate Implement SysUserDaoImpl
 ***********************************************************************/
package org.vsg.security.model.persistence.hibernate;

import java.io.Serializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
import org.springframework.dao.DataAccessException;
import org.vsg.common.pagination.Pagination;
import org.vsg.common.pagination.PaginationException;
import org.vsg.common.pagination.dao.PaginationHibernateDaoSupport;
import org.vsg.common.pagination.datatrans.PaginationProvider;
import org.vsg.security.model.domain.SysUser;
import org.vsg.security.model.persistence.hibernate.proc.SysUserByAllPaginationProc;
import org.vsg.security.model.persistence.hibernate.proc.SysUserPaginationProc;
import org.vsg.security.model.persistence.SysUserDao;

/**
 * @author Bill Vison
 */
public class SysUserDaoImpl extends PaginationHibernateDaoSupport implements SysUserDao {

	/**
	 * set the logger for develope code , do not use System.print(ln) to out print message.
	 */
	private static final Logger logger = LoggerFactory.getLogger(SysUserDaoImpl.class);

	
	/**
	 *
	 * @param sysUser
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.SysUserDao#save(org.vsg.security.model.domain.SysUser)
	 */
	@Override
	public void save(SysUser sysUser) throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().save(sysUser);
	}

	@Override
	public void saveAll(Collection<SysUser> all)
			throws DataAccessException {
		// TODO Auto-generated method stub
		
		getHibernateTemplate().saveOrUpdateAll(all);
		
	}   	
   	
	/**
	 *
	 * @param sysUser
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.SysUserDao#update(org.vsg.security.model.domain.SysUser)
	 */
   	@Override
   	public void update(SysUser sysUser) throws DataAccessException {
   		// TODO Auto-generated method stub
		getHibernateTemplate().update(sysUser);
	}   	


	/**
	 *
	 * @param sysUser
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.SysUserDao#delete(org.vsg.security.model.domain.SysUser)
	 */
	@Override
	public void delete(SysUser sysUser) throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(sysUser);
	}

	@Override
	public void deleteAll(Collection<SysUser> all)
			throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().deleteAll(all);
	}
   	
   	
	/**
	 *
	 * @param pk , the primary key
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.SysUserDao#load(java.io.Serializable)
	 */
	@Override
	public SysUser load(Serializable pk) throws DataAccessException {
		// Start of user code TODO Auto-generated method stub
		SysUser sysUser = (SysUser) getHibernateTemplate().load(SysUser.class, pk);
		
		return sysUser;	
		// End of user code	
	}
   
   
	/**
	 *
	 * @param provider
	 * @exception DataAccessException
	 * @exception PaginationException
	 * @see org.vsg.security.model.persistence.SysUserDao#pageSysUser(org.vsg.common.pagination.datatrans.PaginationProvider)
	 */
	@Override
	public Pagination pageSysUser(PaginationProvider provider) throws DataAccessException, PaginationException {
		// Start of user code TODO Auto-generated method stub
		SysUserPaginationProc proc = new SysUserPaginationProc();
		
		Pagination page = generatePagination(proc , provider);
		
		return page;
		// End of user code	
	}      	


	/**
	 *
	 * @param provider
	 * @exception DataAccessException
	 * @exception PaginationException
	 * @see org.vsg.security.model.persistence.SysUserDao#pageSysUserByAll(org.vsg.common.pagination.datatrans.PaginationProvider)
	 */
	@Override
	public Pagination pageSysUserByAll(PaginationProvider provider) throws DataAccessException, PaginationException {
		// Start of user code TODO Auto-generated method stub
		SysUserByAllPaginationProc proc = new SysUserByAllPaginationProc();
		
		Pagination page = generatePagination(proc , provider);
		
		return page;
		// End of user code		
	}


	/**
	 * @exception DataAccessException
	 */
	@Override
	public Collection<SysUser> findByAll() throws DataAccessException {
		// Start of user code TODO Auto-generated method stub
		StringBuilder query = new StringBuilder("select ");
		query.append("sysUser ");
		query.append("from ").append(SysUser.class.getName()).append(" as sysUser ");
		query.append("where sysUser.flagDeleted = 0 ");
		query.append("order by sysUser.name");
		
		Collection params = new Vector();
		
		logger.info("query : " + query.toString());

		Iterator<SysUser> iter = getHibernateTemplate().iterate(query.toString());
		
		Collection<SysUser> result = new Vector<SysUser>();
		
		while (iter.hasNext()) {
			result.add( iter.next() );
		}
		
		return result;
		// End of user code			
	}

	@Override
	public SysUser findSingleByName(String name) throws DataAccessException {
		// TODO Auto-generated method stub
		StringBuilder query = new StringBuilder("select ");
		query.append("sysUser ");
		query.append("from ").append(SysUser.class.getName()).append(" as sysUser ");
		query.append("where sysUser.name = ? and sysUser.flagDeleted = 0 ");
		
		Collection params = new Vector();
		params.add( name );
		
		logger.debug("query : " + query.toString());

		Iterator<SysUser> iter = getHibernateTemplate().iterate(query.toString()  , params.toArray());

		return iter.next();
	}

	
	
   
} 